﻿
    function alertMsg(p_Msg) {
        if (p_Msg.length > 0) {
            document.getElementById("divHintMsg").innerHTML = p_Msg;
            document.getElementById("divHintMsg").style.display = "";
        }
        else {
            document.getElementById("divHintMsg").style.display = "none";
        }
    }

    function outputResultMsg(p_Msg) {
        if (p_Msg.length > 0) {
            document.getElementById("spanResultMsg").innerHTML = p_Msg;
            document.getElementById("tabResultMsg").style.display = "";
        }
        else {
            document.getElementById("tabResultMsg").style.display = "none";
        }
    }
    
    function appendResultMsg(p_Msg) {
    	document.getElementById("spanResultMsg").innerHTML = document.getElementById("spanResultMsg").innerHTML + "\r\n" + p_Msg;
        document.getElementById("tabResultMsg").style.display = "";
    }
    
    function getChkValues(chkName){
    	var retVal = "";
        var chkbox = document.getElementsByName(chkName); 
        for(var i=0;i<chkbox.length;i++){
             if(chkbox[i].checked){
            	 if(retVal != ""){
            		 retVal+=",";
            	 }
            	 retVal+=chkbox[i].value;
           }
        }
        return retVal;
    }
    
    Date.prototype.format = function(fmt){
      var o = { 
        "M+" : this.getMonth()+1,                 //月份 
        "d+" : this.getDate(),                    //日 
        "h+" : this.getHours(),                   //小时 
        "m+" : this.getMinutes(),                 //分 
        "s+" : this.getSeconds(),                 //秒 
        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
        "S"  : this.getMilliseconds()             //毫秒 
      }; 
      if(/(y+)/.test(fmt)) 
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
      for(var k in o) 
        if(new RegExp("("+ k +")").test(fmt)) 
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 
      return fmt; 
    }
    
    function getHttp(pUrl, fCallback){
    	var callBkArgms = Array.prototype.slice.call(arguments, 2);
    	getOrPost(pUrl,"GET", 20000, null, fCallback, callBkArgms);
    }
    
    function getHttpTimeout(pUrl, timeout, fCallback){
    	var timeoutMil = timeout*1000;
    	var callBkArgms = Array.prototype.slice.call(arguments, 2);
    	getOrPost(pUrl,"GET", timeoutMil, null, fCallback, callBkArgms);
    }
    
    function postHttp(pUrl, pData, fCallback){
    	var callBkArgms = Array.prototype.slice.call(arguments, 3);
    	getOrPost(pUrl,"POST", 20000, pData, fCallback, callBkArgms);
    }
    
    function getOrPost(pUrl, pMethod, pTimeout, pData, fCallback, callBkArgms){
    	try {
            var httpReq = new (window.XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
            httpReq.ontimeout = function() {
            	alertMsg("请求超时");
            }
            httpReq.onreadystatechange = function(){
            	if(httpReq.readyState==4){
            		isRunning = 0;
            		alertMsg("加载结束");
            		if(httpReq.status == 200 || (httpReq.responseText != null && httpReq.responseText.length > 0)){
            			fCallback.apply(httpReq, callBkArgms);
            		}else if(httpReq.status != 200){
                		alertMsg("非正常结束");
                	}else{
                		alertMsg("status:"+httpReq.status);
                	}
            	}
            };
            httpReq.onerror = function(evt) {
            	outputResultMsg("访问服务器出现错误，status："+httpReq.status+"，请确认服务地址："+pUrl+"是否可用");
            };
            httpReq.open(pMethod, pUrl, true);
            httpReq.timeout = pTimeout;
            httpReq.send(pData);
        }
        catch (err) {
            alertMsg("加载失败");
            outputResultMsg("此页面存在一个错误。\n\n错误描述: " + err.description);
        }
    }
    
    function showMsg(pPri){
    	var timeNow = new Date().format("yyyy-MM-dd hh:mm:ss");
    	outputResultMsg(timeNow + " " + pPri + this.responseText);
    }
    
    function showRespInfo(pPri){
    	var timeNow = new Date().format("yyyy-MM-dd hh:mm:ss");
    	try {
    		var idx1 = this.responseText.indexOf("respInfo");
    		if(idx1>0){
    			var jsonTxt = eval('('+this.responseText+')');
    			var respInfo = jsonTxt["respInfo"];
    			outputResultMsg(timeNow + " " + pPri + respInfo);
    		}else{
    			outputResultMsg(timeNow + " " + pPri + this.responseText);
    		}
    	}
        catch (err) {
        	var msg = "此页面存在一个错误。\r\n错误描述: " + err.description;
        	if (this.responseText !== undefined && this.responseText !== null && this.responseText !== ""){
        		msg+="\r\n响应内容："+this.responseText;
        	}
            outputResultMsg(timeNow + " " + pPri + msg);
        }
    }
    
    function selectText(){
    	if(document.Selection){
    		return document.selection.createRange().text;
    	}else{
    		return window.getSelection().toString();
    	}
    }
    
    function showList (trgId) {
    	
    	try {
    		var divTrg = document.getElementById(trgId);
    		//alert(divTrg.childNodes.length);
    		if(divTrg.childNodes.length>0){
    			divTrg.removeChild(divTrg.childNodes[0]);
    		}
    		//document.getElementById("jsonMsg").innerHTML = this.responseText;
    		//var re = new RegExp("\\\\","g");
    		//var jsonTxt = eval('('+this.responseText.replace(re, "\\\\")+')');
    		var jsonTxt = JSON.parse(this.responseText);
    		//alert("xixi2");
    		var respCode = jsonTxt["respCode"];
    		var respInfo = jsonTxt["respInfo"];
    		if("0" == respCode.toString()){
    			
    			var tabData = document.createElement("table");
    			tabData.className = "MyGrid";
    			var rowHd = document.createElement("tr");
    			
    			var columns = jsonTxt.columns;
    			for(var k in columns){
    				var cellHd = document.createElement("td");
    				cellHd.innerHTML = columns[k];
    	            rowHd.appendChild(cellHd);
    			}
    			tabData.appendChild(rowHd);
    			
    			var dataList = jsonTxt.dataList;
    			
    			for(var i=0; i<dataList.length; i++){
    				var eachDataRow = dataList[i];
    				var tmpRow = document.createElement("tr");
    				for(var k in columns){
    		            var tmpCell = document.createElement("td");
    		            if (eachDataRow[columns[k]] !== undefined){
    		            	tmpCell.innerHTML = "<pre>"+eachDataRow[columns[k]]+"</pre>";
    		            }else{
    		            	tmpCell.innerHTML = "[-undefined-]";
    		            }
    		            tmpRow.appendChild(tmpCell);
    		        }
    				tabData.appendChild(tmpRow);
    			}
    			document.getElementById(trgId).appendChild(tabData);
    			
    		}else{
    			outputResultMsg(respInfo);
    		}
    	}
        catch (err) {
        	var msg = "此页面存在一个错误。\r\n错误描述: " + err.description;
        	if (this.responseText !== undefined && this.responseText !== null && this.responseText !== ""){
        		msg+="\r\n响应内容："+this.responseText;
        	}
            outputResultMsg(msg);
        }
    }
